package com.audaque.springboot.foshanupload.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author zgb
 * @desc ...
 * @date 2023-03-02 11:30:37
 */
public class LocalDateTimeUtils {


    /**
     * 获取当前时间毫秒数
     *
     * @return
     */
    public static long getCurrentMilli() {
        return LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    /**
     * 获取当前时间:默认格式yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getCurrentDateStr() {
        LocalDateTime currentDate = LocalDateTime.now();
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DateFormatUtil.DATE_TIME_FORMAT);
        return currentDate.format(dateFormat);
    }

    /**
     * 获取当天时间:默认格式yyyy-MM-dd
     *
     * @return
     */
    public static String getCurrentDayStr() {
        LocalDateTime currentDate = LocalDateTime.now();
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DateFormatUtil.DATE_PATTERN_YYYY_MM_DD);
        return currentDate.format(dateFormat);
    }
    /**
     * 获取明天开头天毫秒数
     *
     * @return
     */
    public static final long getNextDayStartLong() throws ParseException {
        LocalDateTime currentDate = LocalDateTime.now();
        LocalDateTime nextDate = currentDate.plusDays(1);
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DateFormatUtil.DATE_PATTERN_YYYY_MM_DD);
        String str = nextDate.format(dateFormat);
        Date date = DateUtilsExtLang3.parseDate(str, DateFormatUtil.DATE_PATTERN_YYYY_MM_DD);
        return date.getTime();
    }

    /**
     * 获取当前时间
     *
     * @param format
     * @return
     */
    public static final String getCurrentDateStr(String format) {
        if (StringUtils.isEmpty(format)) {
            return getCurrentDateStr();
        }
        LocalDateTime currentDate = LocalDateTime.now();
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(format);
        return currentDate.format(dateFormat);
    }


    /**
     * 获取当天的剩余时间
     *
     * @return
     */
    public static Long surplusTime() {
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(), midnight);
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);

        return seconds;
    }

    /**
     * 获取当天的剩余时间
     *
     * @return
     */
    public static Long mothsurplusTime() {
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(), midnight);
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);

        return seconds;
    }


    /**
     * 两个时间比较大小
     *
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compare_date(String DATE1, String DATE2) {
        if (StringUtils.isEmpty(DATE2)) {
            DATE2 = getCurrentDateStr();
        }
        DateFormat df = new SimpleDateFormat(DateFormatUtil.DATE_TIME_FORMAT);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


    public static Boolean isTodayStatus() {
        Long mothsurplusTime = mothsurplusTime();
        if (mothsurplusTime > 86100) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将 Date 转为 LocalDateTime
     *
     * @param date
     * @return java.time.LocalDateTime;
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

}
